{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "eYaft-dSGj4-"
   },
   "source": [
    "# Computing sentence similarity \n",
    "In this section, let's see learn to compute the semantic similarity between two sentences using the pre-trained Sentence-BERT model. \n",
    "\n",
    "Import the necessary libraries:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "id": "XaX5-msxGlhb"
   },
   "outputs": [],
   "source": [
    "%%capture\n",
    "!pip install sentence_transformers==0.4.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true,
    "id": "RkyNSDXLGj5K"
   },
   "outputs": [],
   "source": [
    "import scipy\n",
    "from sentence_transformers import SentenceTransformer,util"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "eFqrSkVPGj5L"
   },
   "source": [
    "\n",
    "Download and load the pre-trained Sentence-BERT model: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true,
    "id": "2VCjy76TGj5L"
   },
   "outputs": [],
   "source": [
    "model = SentenceTransformer('bert-base-nli-mean-tokens')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "HfuG2Kn5Gj5M"
   },
   "source": [
    "\n",
    "Define the sentence pair: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true,
    "id": "QsSqWyBkGj5M"
   },
   "outputs": [],
   "source": [
    "sentence1 = 'It was a great day' \n",
    "sentence2 = 'Today was awesome'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "TtQccCBfGj5M"
   },
   "source": [
    "\n",
    "Compute the sentence representation of the sentence pair: \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true,
    "id": "s4T01JFjGj5M"
   },
   "outputs": [],
   "source": [
    "sentence1_representation = model.encode(sentence1)\n",
    "sentence2_representation = model.encode(sentence2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "9qxwQ9pTGj5N"
   },
   "source": [
    "\n",
    "Compute the cosine similarity between the two sentence representations:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true,
    "id": "kGev0DFRGj5N"
   },
   "outputs": [],
   "source": [
    "cosine_sim = util.pytorch_cos_sim(sentence1_representation,sentence2_representation)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "cDWvHN-1Gj5N",
    "outputId": "9c4079f2-9cb5-440f-c0d7-fe19f6339804"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0.9313]])\n"
     ]
    }
   ],
   "source": [
    "print(cosine_sim)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "ubF8UfVzGj5O"
   },
   "source": [
    "\n",
    "From the preceding result, we can observe that the given sentence pair is 93% similar. In this way, we can use the pre-trained Sentence-BERT model for sentence similarity tasks. "
   ]
  }
 ],
 "metadata": {
  "colab": {
   "name": "8.05. Computing sentence similarity .ipynb",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
